package com.bocloud.cmp.driver.providers.jd;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.FirewallDriver;
import com.bocloud.cmp.model.FirewallModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;
import com.jcloud.cap.client.ClientConfig;
import com.jcloud.cap.client.IClient;
import com.jcloud.cap.client.JCloudClient;
import com.jcloud.cap.client.builder.v1.CreateFirewallRequestBuilder;
import com.jcloud.cap.client.builder.v1.DeleteFirewallRequestBuilder;
import com.jcloud.cap.client.builder.v1.QueryFirewallDetailRequestBuilder;
import com.jcloud.cap.client.builder.v1.QueryFirewallsRequestBuilder;
import com.jcloud.cap.client.exception.ClientException;
import com.jcloud.cap.client.exception.ServerException;
import com.jcloud.cap.client.http.HttpConfig;
import com.jcloud.cap.data.model.v1.CreateFirewallResponse;
import com.jcloud.cap.data.model.v1.DeleteFirewallResponse;
import com.jcloud.cap.data.model.v1.FirewallResponse.Firewall;
import com.jcloud.cap.data.model.v1.QueryFirewallDetailResponse;
import com.jcloud.cap.data.model.v1.QueryFirewallsResponse;

public class JdFirewallDriver extends JdDriver implements FirewallDriver {
	private static Logger logger = LoggerFactory.getLogger(JdFirewallDriver.class);
	private IClient client;

	/**
	 * @param accessKey
	 * @param secretKey
	 * @param endpoint
	 */
	public JdFirewallDriver(String accessKey, String secretKey, String endpoint, String region) {
		super(accessKey, secretKey, endpoint, region);
		ClientConfig config = new ClientConfig();
		HttpConfig httpConfig = new HttpConfig();
		// 调用地址
		httpConfig.setEndpoint(endpoint);
		httpConfig.setReadTimeout(20000);
		httpConfig.setConnectTimeout(20000);
		config.setHttpConfig(httpConfig);
		config.setCredential(this.getCredential().getCapCredential());
		client = new JCloudClient(config);
	}

	@Override
	public BsmResult list() {
		QueryFirewallsRequestBuilder builder = QueryFirewallsRequestBuilder.create();
		this.init(builder, "network", "queryFirewalls", this.getRegion());
		try {
			QueryFirewallsResponse response = client.send(builder.build());
			List<FirewallModel> models = Lists.newArrayList();
			List<Firewall> firewalls = response.getFirewalls();
			if (null != firewalls) {
				for (Firewall firewall : firewalls) {
					FirewallModel model = new FirewallModel(firewall.getFirewallId(), firewall.getFirewallName(),
							firewall.getDescription(), firewall.getFirewallPolicyId(), firewall.getFirewallStatus(),
							firewall.getCreateTime(), null);
					models.add(model);
				}
			}
			return new BsmResult(true, models, "success");
		} catch (ClientException | ServerException e) {
			logger.error("List firewall [{}] exception!", e);
			return new BsmResult(false, "List firewall failed");
		}
	}

	@Override
	public BsmResult create(FirewallModel firewall) {
		CreateFirewallRequestBuilder builder = CreateFirewallRequestBuilder.create();
		this.init(builder, "network", "createFirewall", this.getRegion());
		try {
			CreateFirewallResponse response = client.send(builder.name(firewall.getName())
					.policyId(firewall.getPolicyId()).description(firewall.getRemark()).build());
			return detail(response.getFirewallId(), response.getRequestId());
		} catch (ClientException | ServerException e) {
			logger.error("Create firewall [{}] exception!", e);
			return new BsmResult(false, "Create firewall failed");
		}

	}

	@Override
	public BsmResult remove(String firewallId) {
		DeleteFirewallRequestBuilder builder = DeleteFirewallRequestBuilder.create();
		this.init(builder, "network", "deleteFirewall", this.getRegion());
		try {
			DeleteFirewallResponse response = client.send(builder.firewallId(firewallId).build());
			return new BsmResult(true, response.getRequestId(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Reomve firewall [{}] exception!", e);
			return new BsmResult(false, "Reomve firewall failed");
		}
	}

	@Override
	public BsmResult detail(String firewallId) {
		return detail(firewallId, null);
	}

	private BsmResult detail(String firewallId, String requestId) {
		QueryFirewallDetailRequestBuilder builder = QueryFirewallDetailRequestBuilder.create();
		this.init(builder, "network", "queryFirewallDetail", this.getRegion());
		try {
			QueryFirewallDetailResponse response = client.send(builder.firewallId(firewallId).build());
			Firewall firewall = response.getFirewall();
			if (null == firewall) {
				return new BsmResult(false, "该对象不存在");
			}
			FirewallModel model = new FirewallModel(firewall.getFirewallId(), firewall.getFirewallName(),
					firewall.getDescription(), firewall.getFirewallPolicyId(), firewall.getFirewallStatus(),
					firewall.getCreateTime(), requestId);
			return new BsmResult(true, model, "success");
		} catch (ClientException | ServerException e) {
			logger.error("Query firewall [{}] exception!", e);
			return new BsmResult(false, "Query firewall failed");
		}
	}
}
